//
//  YWMaterialInfoTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialInfoTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"

#import "YWMaterial.h"
#import "YWMaterialApply.h"
#import "YWMaterialApplyDetail.h"
#import "YWMaterialStockOutDetail.h"
#import "YWMaterialReturnDetail.h"
#import "YWMaterialStock.h"
#import "YWBuilding.h"
#import "YWDBTool.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YYLabel.h"
#import "YWCommonTool.h"

@interface YWMaterialInfoTableViewCell()
/* 物料名称 */
@property (nonatomic, strong) YYLabel * materialNameLabel;
/* 品牌 */
@property (nonatomic, strong) YYLabel * BrandsLabel;
/* 规格 */
@property (nonatomic, strong) YYLabel * specificationLabel;
/* 简码 */
@property (nonatomic, strong) YYLabel * shortCodeLabel;
/* 数量 */
@property (nonatomic, strong) YYLabel * numbelLabel;
/* 物料状态（是否领用） */
@property (nonatomic, strong) YYLabel * materialStateLabel;
/* 位置 */
@property (nonatomic, strong) YYLabel * locationLabel;
@end

@implementation YWMaterialInfoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubviews{
    /* 名称 */
    [self.materialNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(SCREEN_WIDTH/2-10);
    }];
    /* 规格 */
    [self.specificationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.materialNameLabel.mas_right);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    /* 品牌 */
    [self.BrandsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.materialNameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(SCREEN_WIDTH/2-10);
    }];
    /* 数量 */
    [self.numbelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.specificationLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.BrandsLabel.mas_right);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.BrandsLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.and.bottom.mas_equalTo(self.contentView).offset(-10);
    }];
}

- (void)setMaterialStock:(YWMaterialStock *)materialStock{
    _materialStock = materialStock;
    if ([self.selectedArray containsObject:materialStock]) {
        NSString * numbel = [NSString stringWithFormat:@"%ld%@",(long)_materialStock.selectQuantity,_materialStock.materialInfo.unit?:@""];
        _numbelLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"已选数量: " firstColor:[UIColor customTextGrayColor] secondText:numbel secondColor:[UIColor customBlueColor] font:kContentFont];
        NSString*materialName = _materialStock.materialInfo.materialName;
        if (materialName.length>0) {
            _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:materialName secondColor:[UIColor customBlueColor] font:kContentFont];
        }else{
            _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor customBlueColor] font:kContentFont];
        }
        NSString*model = _materialStock.materialInfo.model;
        if (model.length>0) {
            _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:model secondColor:[UIColor customBlueColor] font:kContentFont];
        }else{
            _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor customBlueColor] font:kContentFont];
        }
        NSString*brand = _materialStock.materialInfo.brand;
        if (brand.length>0) {
            _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:brand secondColor:[UIColor customBlueColor] font:kContentFont];
        }else{
            _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor customBlueColor] font:kContentFont];
        }
        
        YWBuilding * building = [[YWDBTool shareDBTool] buildingWithId:_materialStock.buildingId];
        NSString * location = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",_materialStock.apartment.apartmentName?:@"",_materialStock.floor.floorName?:@"",_materialStock.location.locationName?:@""];
        _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayColor] secondText:location secondColor:[UIColor customBlueColor] font:kContentFont];
    }else{
        NSString * numbel = [NSString stringWithFormat:@"%ld%@",(long)_materialStock.quantity,_materialStock.materialInfo.unit?:@""];
        _numbelLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"数量: " firstColor:[UIColor customTextGrayColor] secondText:numbel secondColor:[UIColor blackColor] font:kContentFont];
        NSString*materialName = _materialStock.materialInfo.materialName;
        if (materialName.length>0) {
            _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:materialName secondColor:[UIColor blackColor] font:kContentFont];
        }else{
            _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
        }
        NSString*model = _materialStock.materialInfo.model;
        if (model.length>0) {
            _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:model secondColor:[UIColor blackColor] font:kContentFont];
        }else{
            _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
        }
        NSString*brand = _materialStock.materialInfo.brand;
        if (brand.length>0) {
            _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:brand  secondColor:[UIColor blackColor] font:kContentFont];
        }else{
            _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
        }
        
        YWBuilding * building = [[YWDBTool shareDBTool] buildingWithId:_materialStock.buildingId];
        NSString * location = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",_materialStock.apartment.apartmentName?:@"",_materialStock.floor.floorName?:@"",_materialStock.location.locationName?:@""];
        _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayColor] secondText:location secondColor:[UIColor blackColor] font:kContentFont];
    }    
}

- (void)setApplyDetail:(YWMaterialApplyDetail *)applyDetail
{
    _applyDetail = applyDetail;
    NSString*materialName = _applyDetail.materialStock.materialInfo.materialName;
    if (materialName.length>0) {
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:materialName secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString* model =_applyDetail.materialStock.materialInfo.model;
    if (model.length>0) {
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:model secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString* brand = _applyDetail.materialStock.materialInfo.brand;
    if (brand.length>0) {
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:brand secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString * numbel = [NSString stringWithFormat:@"%ld%@",(long)_applyDetail.quantity,_applyDetail.materialStock.materialInfo.unit?:@""];
    _numbelLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"数量: " firstColor:[UIColor customTextGrayColor] secondText:numbel  secondColor:[UIColor blackColor] font:kContentFont];
    
    YWBuilding * building = [[YWDBTool shareDBTool] buildingWithId:_applyDetail.materialStock.buildingId];
    NSString * location = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",_applyDetail.materialStock.apartment.apartmentName?:@"",_applyDetail.materialStock.floor.floorName?:@"",_applyDetail.materialStock.location.locationName?:@""];
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayColor] secondText:location  secondColor:[UIColor blackColor] font:kContentFont];
}

- (void)setStockOutDetail:(YWMaterialStockOutDetail *)stockOutDetail
{
    _stockOutDetail = stockOutDetail;
    NSString*materialName = _stockOutDetail.materialStock.materialInfo.materialName;
    if (materialName.length>0) {
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:materialName  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString*model = _stockOutDetail.materialStock.materialInfo.model;
    if (model.length>0) {
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:model  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString*brand = _stockOutDetail.materialStock.materialInfo.brand;
    if (brand.length>0) {
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:brand  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString * numbel = [NSString stringWithFormat:@"%ld%@",(long)_stockOutDetail.quantity,_stockOutDetail.materialStock.materialInfo.unit?:@""];
    _numbelLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"数量: " firstColor:[UIColor customTextGrayColor] secondText:numbel  secondColor:[UIColor blackColor] font:kContentFont];
    YWBuilding * building = [[YWDBTool shareDBTool] buildingWithId:_stockOutDetail.materialStock.buildingId];
    NSString * location = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",_stockOutDetail.materialStock.apartment.apartmentName?:@"",_stockOutDetail.materialStock.floor.floorName?:@"",_stockOutDetail.materialStock.location.locationName?:@""];
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayColor] secondText:location  secondColor:[UIColor blackColor] font:kContentFont];
}

- (void)setReturnDetail:(YWMaterialReturnDetail *)returnDetail
{
    _returnDetail = returnDetail;
    NSString*materialName = _returnDetail.materialStock.materialInfo.materialName;
    if (materialName.length>0) {
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:materialName secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _materialNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"名称: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString* model = _returnDetail.materialStock.materialInfo.model;
    if (model.length>0) {
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:model  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _specificationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"规格: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString*brand = _returnDetail.materialStock.materialInfo.brand;
    if (brand.length>0) {
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:brand  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _BrandsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"品牌: " firstColor:[UIColor customTextGrayColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    NSString * numbel = [NSString stringWithFormat:@"%ld%@",(long)_returnDetail.quantity,_returnDetail.materialStock.materialInfo.unit?:@""];
    _numbelLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"数量: " firstColor:[UIColor customTextGrayColor] secondText:numbel  secondColor:[UIColor blackColor] font:kContentFont];
    
    YWBuilding * building = [[YWDBTool shareDBTool] buildingWithId:_returnDetail.materialStock.buildingId];
    NSString * location = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",_returnDetail.materialStock.apartment.apartmentName?:@"",_returnDetail.materialStock.floor.floorName?:@"",_returnDetail.materialStock.location.locationName?:@""];
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayColor] secondText:location  secondColor:[UIColor blackColor] font:kContentFont];
}

- (YYLabel *)materialNameLabel{
    if (!_materialNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _materialNameLabel = label;
        [self.contentView addSubview:_materialNameLabel];
    }
    return _materialNameLabel;
}

- (YYLabel *)BrandsLabel{
    if (!_BrandsLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _BrandsLabel = label;
        [self.contentView addSubview:_BrandsLabel];
    }
    return _BrandsLabel;
}

- (YYLabel *)specificationLabel{
    if (!_specificationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _specificationLabel = label;
        [self.contentView addSubview:_specificationLabel];
    }
    return _specificationLabel;
}

- (YYLabel *)shortCodeLabel{
    if (!_shortCodeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _shortCodeLabel = label;
        [self.contentView addSubview:_shortCodeLabel];
    }
    return _shortCodeLabel;
}

- (YYLabel *)numbelLabel{
    if (!_numbelLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _numbelLabel = label;
        [self.contentView addSubview:_numbelLabel];
    }
    return _numbelLabel;
}

- (YYLabel *)materialStateLabel{
    if (!_materialStateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _materialStateLabel = label;
        [self.contentView addSubview:_materialStateLabel];
    }
    return _materialStateLabel;
}

- (YYLabel *)locationLabel{
    if (!_locationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _locationLabel = label;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

@end
